package znyl.nursing.controller.member;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import znyl.common.core.controller.BaseController;
import znyl.common.core.domain.AjaxResult;
import znyl.common.core.domain.R;
import znyl.common.core.page.TableDataInfo;
import znyl.common.utils.UserThreadLocal;
import znyl.nursing.domain.Reservation;
import znyl.nursing.dto.ReservationDto;
import znyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@Api(tags = "参观预约")
@RestController
@RequestMapping("/member/reservation")
public class MemberReservationController extends BaseController {

        @Autowired
        private IReservationService reservationService;


        @GetMapping("/cancelled-count")
        @ApiOperation("查询取消预约数量")
        public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();// 获取当前用户ID
            System.out.println("------"+userId);
        Integer count = reservationService.getCancelledReservationCo(userId);

        return R.ok(count);

    }
        @PostMapping
        @ApiOperation("新增预约")
    public AjaxResult add(@RequestBody ReservationDto reservationDto) {
            Reservation bean = BeanUtil.toBean(reservationDto, Reservation.class);
            Long userId = UserThreadLocal.getUserId();// 获取当前用户ID
            bean.setUpdateBy(String.valueOf(userId));
            return toAjax(reservationService.insertReservation(bean));

    }
    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public AjaxResult page(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "status", defaultValue = "1", required = false) Integer status
                           ){
        IPage<Reservation> page1 =new Page<>(pageNum,pageSize);
        TableDataInfo list =reservationService.Ipage(page1,status);
        return  AjaxResult.success(list);

    }
    /**
     * 取消预约
     */
    @DeleteMapping("/{id}")
    @ApiOperation("取消预约")
    public AjaxResult cancel(@PathVariable Long id) {
        return toAjax(reservationService.cancelReservation(id));
    }
}
